package com.domor.web.controller.system;

import java.util.List;

import com.domor.system.domain.SysUser;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.domor.common.annotation.Log;
import com.domor.common.core.controller.BaseController;
import com.domor.common.core.domain.AjaxResult;
import com.domor.common.core.page.TableDataInfo;
import com.domor.framework.shiro.util.ShiroUtils;
import com.domor.system.domain.SysNotice;
import com.domor.system.service.SysNoticeService;

/**
 * <h3>公告 控制层</h3>
 * @author liyuyang
 * @date 2020/5/11
 **/
@Controller
@RequestMapping("/system/notice")
public class SysNoticeController extends BaseController {
    private String prefix = "system/notice";

    @Autowired
    private SysNoticeService noticeService;

    @GetMapping()
    public String notice() {
        return prefix + "/index";
    }

    /**
     * 查询公告列表
     */
    @RequiresPermissions("system:notice:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysNotice notice) {
        setOrderByAndPage();
        List<SysNotice> list = noticeService.selectNoticeList(notice);
        return getDataTable(list);
    }

    /**
     * 新增公告
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存公告
     */
    @RequiresPermissions("system:notice:add")
    @Log(title = "通知公告")
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysNotice notice) throws Exception{
        SysUser user = ShiroUtils.getSysUser();
        notice.setCreateBy(ShiroUtils.getLoginName());
        return toAjax(noticeService.insertNotice(notice, user));
    }

    /**
     * 修改公告
     */
    @GetMapping("/edit/{noticeId}")
    public String edit(@PathVariable("noticeId") Integer noticeId, ModelMap mmap) {
        mmap.put("notice", noticeService.selectNoticeById(noticeId));
        return prefix + "/edit";
    }

    /**
     * 修改保存公告
     */
    @RequiresPermissions("system:notice:edit")
    @Log(title = "通知公告")
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SysNotice notice) {
        notice.setUpdateBy(ShiroUtils.getLoginName());
        return toAjax(noticeService.updateNotice(notice));
    }

    /**
     * 删除公告
     */
    @RequiresPermissions("system:notice:delete")
    @Log(title = "通知公告")
    @PostMapping("/delete/{noticeId}")
    @ResponseBody
    public AjaxResult delete(@PathVariable("noticeId") Integer noticeId) {
        return toAjax(noticeService.deleteNoticeById(noticeId));
    }

}
